/*
 * CalendarPanel.java
 *
 * Created on Oct 19, 2011, 1:35:24 PM
 */
package carassius.GUI.Calendar;

import carassius.BLL.Events.ArrayListWithEvents;
import carassius.BLL.GregorianCalendarWithEvents;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.GregorianCalendar;

/**
 *
 * @author siebz0r
 */
public class CalendarPanel
		extends javax.swing.JPanel
		implements PropertyChangeListener
{
	//<editor-fold defaultstate="collapsed" desc="Fields">
	private GregorianCalendarWithEvents _calendar;
	private ArrayListWithEvents<Long> _markedDates;
	//</editor-fold>

	//<editor-fold defaultstate="collapsed" desc="Constructors">
	public CalendarPanel()
	{
		this(new GregorianCalendarWithEvents());
	}

	public CalendarPanel(int year, int month)
	{
		this(new GregorianCalendarWithEvents(year, month, 1));
	}

	public CalendarPanel(ArrayListWithEvents<Long> markedDates)
	{
		this(new GregorianCalendarWithEvents(), markedDates);
	}

	public CalendarPanel(GregorianCalendarWithEvents calendar)
	{
		this(calendar, new ArrayListWithEvents<Long>());
	}

	public CalendarPanel(GregorianCalendarWithEvents calendar,
						 ArrayListWithEvents<Long> markedDates)
	{
		_calendar = calendar;
		_markedDates = markedDates;
		_calendar.addPropertyChangeListener("year", this);
		_calendar.addPropertyChangeListener("month", this);
		_calendar.addPropertyChangeListener("day", this);
		_calendar.addPropertyChangeListener("firstDayOfWeek", this);
		initComponents();
	}
	//</editor-fold>

	//<editor-fold defaultstate="collapsed" desc="Public Methods">
	public void setDate(int year, int month, int day)
	{
		_calendar.set(year, month, day);
	}

	public GregorianCalendar getDate()
	{
		return new GregorianCalendar(getYear(), getMonth(), getDay());
	}

	public void setDay(int day)
	{
		_calendar.set(GregorianCalendar.DATE, day);
	}

	public int getDay()
	{
		return _calendar.get(GregorianCalendar.DATE);
	}

	public void setMonth(int month)
	{
		_calendar.set(GregorianCalendar.MONTH, month);
	}

	public int getMonth()
	{
		return _calendar.get(GregorianCalendar.MONTH);
	}

	public void setYear(int year)
	{
		_calendar.set(GregorianCalendar.YEAR, year);
	}

	public int getYear()
	{
		return _calendar.get(GregorianCalendar.YEAR);
	}

	public boolean addMarkedDate(long date)
	{
		return _markedDates.add(date);
	}

	public boolean removeMarkedDate(long date)
	{
		return _markedDates.remove(date);
	}

	public ArrayList<Long> getMarkedDates()
	{
		return (ArrayList<Long>) _markedDates.clone();
	}
	//</editor-fold>

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        uxMonthPanel = new carassius.GUI.Calendar.MonthPanel(_calendar);
        uxYearSpinner = new carassius.GUI.Calendar.YearSpinner(_calendar);
        uxMonthViewContainerPanel = new javax.swing.JPanel();
        uxTodayButton = new javax.swing.JButton();
        uxWeekHeaderPanel = new carassius.GUI.Calendar.WeekHeaderPanel(_calendar);
        uxDayNamesHeaderPanel = new carassius.GUI.Calendar.DayNamesHeaderPanel(_calendar);
        uxMonthViewPanel = new carassius.GUI.Calendar.MonthViewPanel(_calendar, _markedDates);

        setName("Form"); // NOI18N
        setLayout(new java.awt.GridBagLayout());

        uxMonthPanel.setName("uxMonthPanel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        add(uxMonthPanel, gridBagConstraints);

        uxYearSpinner.setMaximumSize(new java.awt.Dimension(70, 21));
        uxYearSpinner.setMinimumSize(new java.awt.Dimension(70, 21));
        uxYearSpinner.setName("uxYearSpinner"); // NOI18N
        uxYearSpinner.setPreferredSize(new java.awt.Dimension(70, 21));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        add(uxYearSpinner, gridBagConstraints);

        uxMonthViewContainerPanel.setName("uxMonthViewContainerPanel"); // NOI18N
        uxMonthViewContainerPanel.setLayout(new java.awt.GridBagLayout());

        uxTodayButton.setName("uxTodayButton"); // NOI18N
        uxTodayButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                uxTodayButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        uxMonthViewContainerPanel.add(uxTodayButton, gridBagConstraints);

        uxWeekHeaderPanel.setName("uxWeekHeaderPanel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        uxMonthViewContainerPanel.add(uxWeekHeaderPanel, gridBagConstraints);

        uxDayNamesHeaderPanel.setName("uxDayNamesHeaderPanel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        uxMonthViewContainerPanel.add(uxDayNamesHeaderPanel, gridBagConstraints);

        uxMonthViewPanel.setName("uxMonthViewPanel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        uxMonthViewContainerPanel.add(uxMonthViewPanel, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.gridheight = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        add(uxMonthViewContainerPanel, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

	private void uxTodayButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_uxTodayButtonActionPerformed
	{//GEN-HEADEREND:event_uxTodayButtonActionPerformed
		GregorianCalendar cal = new GregorianCalendar();
		setYear(cal.get(GregorianCalendar.YEAR));
		setMonth(cal.get(GregorianCalendar.MONTH));
		setDay(cal.get(GregorianCalendar.DATE));
	}//GEN-LAST:event_uxTodayButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private carassius.GUI.Calendar.DayNamesHeaderPanel uxDayNamesHeaderPanel;
    private carassius.GUI.Calendar.MonthPanel uxMonthPanel;
    private javax.swing.JPanel uxMonthViewContainerPanel;
    private carassius.GUI.Calendar.MonthViewPanel uxMonthViewPanel;
    private javax.swing.JButton uxTodayButton;
    private carassius.GUI.Calendar.WeekHeaderPanel uxWeekHeaderPanel;
    private carassius.GUI.Calendar.YearSpinner uxYearSpinner;
    // End of variables declaration//GEN-END:variables

	@Override
	public void propertyChange(PropertyChangeEvent evt)
	{
		if (evt.getSource() == _calendar)
		{
			firePropertyChange(evt.getPropertyName(), evt.getOldValue(), evt.getNewValue());
		}
	}
}
